package cn.itcast.bos.web.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.StandardService;
import cn.itcast.bos.service.impl.StandardServiceImpl;
@ParentPackage(value = "json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	private static final long serialVersionUID = 1L;
	//模型驱动
	private Standard standard=new Standard();
	//注入service
	@Autowired
	private StandardService standardService;
	@Override
	public Standard getModel() {
		return standard;
	}
	/**
	 * 
	 * 保存方法
	 * @return
	 */
	
	@Action(value="standard_save",results= {@Result(name="success",type="redirect", location="./pages/base/standard.html")})
	public String save() {
		standardService.save(standard);
		return SUCCESS;
	}
	/**
	 * 
	 * 对取派标准名称
	 * @return
	 */
	@Action(value="standard_validate",results= {@Result(name="success",type="json")})
	public String validateName() {
		//获取页面传递的值
		String name = standard.getName();
		List<Standard> list = standardService.findByName(name);
		if (list!=null&&list.size()==0) {
			ServletActionContext.getContext().getValueStack().push(true);
		}else {
			ServletActionContext.getContext().getValueStack().push(false);
		}
		return SUCCESS;
	}
	
	/**
	 * 分页查询,返回json
	 * 
	 */
	
		public int page;
		public int rows;
		public void setPage(int page) {
			this.page = page;
			System.out.println(page);
		}

		public void setRows(int rows) {
			this.rows = rows;
			System.out.println(rows);
		}

		@Action(value="standard_pageQuery",results= {@Result(name="success",type="json")})
		public String pageQuery() {
			//调用业务层,查询结果
			Pageable pageable = new PageRequest(page-1, rows);
			Page<Standard> pageData =standardService.findPageData(pageable);
			//获取返回数据,变为json,传给前端页面
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("total", pageData.getTotalElements());
			map.put("rows", pageData.getContent());
			ActionContext.getContext().getValueStack().push(map);
			return SUCCESS;
		}
		
		/**
		 * 删除操作
		 * 
		 */
		private String ids;
		
		public void setIds(String ids) {
			this.ids = ids;
		}
		@Action(value="standard_delete",results= {@Result(name="success",type="redirect", location="./pages/base/standard.html")})
		public String delete() {
			String[] arrayIds = ids.split(",");
			standardService.deleteStandardById(arrayIds);
			return SUCCESS;
		}
		/**
		 * 查询所有
		 * @return
		 */
		@Action(value="standard_findAll",results= {@Result(name="success",type="json")})
		public String findAll() {
			//调用业务层,查询数据结果
			List<Standard> list=standardService.findAll();
			ActionContext.getContext().getValueStack().push(list);
			return SUCCESS;
		}

}








